package com.winit.openapi.lma.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.common.query.Page;
import com.winit.common.query.Searchable;
import com.winit.common.spi.context.CommandContext;
import com.winit.oms.spi.lma.LMAShipmentOrderService;
import com.winit.oms.spi.lma.command.BatchCreateShipmentOrderCommand;
import com.winit.oms.spi.lma.command.BatchPrintShipmentOrderCommand;
import com.winit.oms.spi.lma.command.CancelShipmentOrderCommand;
import com.winit.oms.spi.lma.command.ConfirmShipmentOrderCommand;
import com.winit.oms.spi.lma.command.CreateShipmentOrderCommand;
import com.winit.oms.spi.lma.command.GetShipmentOrderTrackingMsgCommand;
import com.winit.oms.spi.lma.command.PrintShipmentOrderCommand;
import com.winit.oms.spi.lma.command.QueryShipmentOrderBySellerCommand;
import com.winit.oms.spi.lma.command.QueryShipmentOrderByTrackingNoCommand;
import com.winit.oms.spi.lma.command.QueryShipmentOrderCommand;
import com.winit.oms.spi.lma.command.QueryShipmentOrderLogBySellerCommand;
import com.winit.oms.spi.lma.vo.LMAShipmentOrderImportLogVo;
import com.winit.oms.spi.lma.vo.LMAShipmentOrderLabelVo;
import com.winit.oms.spi.lma.vo.LMAShipmentOrderVo;
import com.winit.oms.spi.lma.vo.LMAShipmentOrderWorkOrderHelpVo;
import com.winit.oms.spi.vo.CreateOrderResult;
import com.winit.oms.spi.vo.v2.order.OrderCancelVo;
import com.winit.oms.spi.vo.v2.response.ErrorResult;
import com.winit.oms.spi.vo.v2.response.ModifyResponse;
import com.winit.openapi.constants.ApiConstant;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.lma.controller.builder.LMAShipmentOrderBuilder;
import com.winit.openapi.lma.controller.builder.LMAShipmentOrderLogBuilder;
import com.winit.openapi.lma.controller.validator.LMAShipmentOrderValidator;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.DateUtil;
import com.winit.openapi.util.PDFBox4MergePDF;
import com.winit.openapi.util.ValidateUtil;
import com.winit.openapi.util.WinitLabelParser;

@Controller
@RequestMapping(value = "/lma/shipment")
public class LMAShipmentOrderController extends BaseController {

    private Logger                  logger = LoggerFactory.getLogger(LMAShipmentOrderController.class);

    @Resource
    private LMAShipmentOrderService lmaShipmentOrderService;

    @RequestMapping(value = "/create")
    public String create(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        LMAShipmentOrderVo shipmentVo = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            LMAShipmentOrderValidator.validateCreateShipmentOrder(json);
            shipmentVo = LMAShipmentOrderBuilder.buildCreateShipmentOrder(json);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        CreateShipmentOrderCommand command = new CreateShipmentOrderCommand();
        command.setShipmentVo(shipmentVo);
        command.setCtx(CommandContext.getContext());

        CreateOrderResult result = lmaShipmentOrderService.createShipmentOrder(command);

        if (result.isSuccess()) {
            // 返回订单号
            Map<String, String> data = new HashMap<String, String>();
            data.put("orderNo", result.getOrderNo());
            // data.put("trackingNo", result.getTrackingNo());
            responseMsg.setData(data);
        } else {
            responseMsg.setData(result.getMessageList());
            throw new ApiException(ErrorCode.VALIDTE_ORDER_FAILED);
        }
        return SUCCESS;
    }

    @RequestMapping(value = "/print")
    public String print(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        String winitOrderNo = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            winitOrderNo = json.getString("winitOrderNo");
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        PrintShipmentOrderCommand command = new PrintShipmentOrderCommand();
        command.setCtx(CommandContext.getContext());
        command.setOrderNo(winitOrderNo);

        List<LMAShipmentOrderLabelVo> labelVos = lmaShipmentOrderService.printShipmentOrderLabel(command);

        if (labelVos == null || labelVos.size() == 0) {
            throw new ApiException(ErrorCode.LABEL_NOT_CREATE);
        } else if (labelVos.size() == 1) {
            LMAShipmentOrderLabelVo labelVo = labelVos.get(0);

            if ("FD".equals(labelVo.getLabelStatus())) {
                throw new ApiException(ErrorCode.ERP_ERROR, labelVo.getLabelDescription());
            }

            WinitLabelParser parser = new WinitLabelParser();
            String base64 = parser.getWinitLabelByURL(labelVo.getLabelUrl());

            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put("label", base64);
            String url = labelVo.getLabelUrl();
            if (StringUtils.isNotBlank(url)) {
                String formatType = url.substring(url.lastIndexOf(".") + 1, url.length());
                if (StringUtils.isNotBlank(formatType)) {
                    dataMap.put("formatType", formatType.toLowerCase());
                }
                dataMap.put("trackingNo", labelVo.getTrackingNo());
            }

            List<Map<String, Object>> labelList = new ArrayList<Map<String, Object>>();
            labelList.add(dataMap);

            Map<String, Object> data = new HashMap<String, Object>();
            data.put("labelList", labelList);
            responseMsg.setData(data);
        } else {
            throw new ApiException(ErrorCode.OP_NOT_SUPORT);
        }

        return SUCCESS;
    }

    @RequestMapping(value = "/get")
    public String get(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        List<String> winitOrderNOs = new ArrayList<String>();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            JSONArray arr = json.getJSONArray("winitOrderNOs");
            for (int i = 0; i < arr.size(); i++) {
                String j = arr.getString(i);
                winitOrderNOs.add(j);
            }
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        GetShipmentOrderTrackingMsgCommand command = new GetShipmentOrderTrackingMsgCommand();
        command.setCtx(CommandContext.getContext());
        command.setOrderNos(winitOrderNOs);
        List<LMAShipmentOrderLabelVo> vos = lmaShipmentOrderService.getOrderTrackingMsg(command);

        Map<String, Object> data = new HashMap<String, Object>();
        List<Map<String, Object>> labels = new ArrayList<Map<String, Object>>();
        if (vos != null) {
            for (LMAShipmentOrderLabelVo vo : vos) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("winitOrderNo", vo.getOrderNo());
                map.put("trackingNo", vo.getTrackingNo());
                map.put("status", "SC".equals(vo.getLabelStatus()) ? "Y" : "N");
                map.put("msg", vo.getLabelDescription());
                labels.add(map);
            }
        }
        data.put("orderList", labels);
        responseMsg.setData(data);
        return SUCCESS;
    }

    @RequestMapping(value = "/confirm")
    public String confirm(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        List<String> winitOrderNOs = new ArrayList<String>();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            JSONArray arr = json.getJSONArray("winitOrderNOs");
            for (int i = 0; i < arr.size(); i++) {
                String j = arr.getString(i);
                winitOrderNOs.add(j);
            }
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        ConfirmShipmentOrderCommand command = new ConfirmShipmentOrderCommand();
        command.setCtx(CommandContext.getContext());
        command.setOrderNos(winitOrderNOs);
        ModifyResponse resp = lmaShipmentOrderService.confirmShipmentOrder(command);

        Map<String, Object> data = new HashMap<String, Object>();
        if (resp != null) {
            data.put("successCount", resp.getSuccessNum());
            data.put("failedCount", resp.getFailNum());
            List<ErrorResult> errors = resp.getErrorResultList();
            if (errors != null && errors.size() > 0) {
                List<Map<String, Object>> errorMsgs = new ArrayList<Map<String, Object>>();
                for (ErrorResult error : errors) {
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("orderNo", error.getErrorOrderNo());
                    map.put("errorCode", error.getErrorCode());
                    map.put("errorMsg", error.getErrorMsg());
                    errorMsgs.add(map);
                }
                data.put("errorMsgs", errorMsgs);
            }
            responseMsg.setData(data);
        }
        return SUCCESS;
    }

    @RequestMapping(value = "/cancel")
    public String cancel(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {

        Object obj = requestMsg.getData();
        List<OrderCancelVo> vos = new ArrayList<OrderCancelVo>();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            JSONArray arr = json.getJSONArray("winitOrderNOs");
            String cancelReason = json.getString("cancelReason");
            for (int i = 0; i < arr.size(); i++) {
                String j = arr.getString(i);
                OrderCancelVo vo = new OrderCancelVo();
                vo.setOrderNo(j);
                vo.setCancelReason(cancelReason);
                vos.add(vo);
            }
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        CancelShipmentOrderCommand command = new CancelShipmentOrderCommand();
        command.setCtx(CommandContext.getContext());
        command.setCancelVos(vos);
        ModifyResponse resp = lmaShipmentOrderService.cancelShipmentOrder(command);
        Map<String, Object> data = new HashMap<String, Object>();
        if (resp != null) {
            data.put("successCount", resp.getSuccessNum());
            data.put("failedCount", resp.getFailNum());
            List<ErrorResult> errors = resp.getErrorResultList();
            if (errors != null && errors.size() > 0) {
                List<String> errorMsgs = new ArrayList<String>();
                for (ErrorResult error : errors) {
                    errorMsgs.add(error.getErrorMsg());
                }
                data.put("errorMsg", errorMsgs);
            }
            responseMsg.setData(data);
        }
        return SUCCESS;
    }

    @RequestMapping(value = "/track")
    public String track(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {

        return null;
    }

    @RequestMapping(value = "/batchImport")
    public String batchImport(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        List<LMAShipmentOrderVo> list = new ArrayList<LMAShipmentOrderVo>();
        String importExcelName = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            JSONArray array = json.getJSONArray("orderList");
            importExcelName = json.getString("importExcelName");
            for (int i = 0; i < array.size(); i++) {
                List<ErrorResult> errors = new ArrayList<ErrorResult>();
                try {
                    JSONObject jsonObject = (JSONObject) array.get(i);
                    LMAShipmentOrderValidator.validateCreateShipmentOrder(jsonObject);
                    LMAShipmentOrderVo shipmentVo = LMAShipmentOrderBuilder.buildCreateShipmentOrder(jsonObject);
                    list.add(shipmentVo);
                } catch (ApiException e) {
                    ErrorResult result = new ErrorResult();
                    result.setErrorCode(e.getErrorCode());
                    result.setErrorMsg(e.getErrorMsg());
                    result.setErrorIndex(i+2);
                    errors.add(result);
                } catch (Exception e) {
                    ErrorResult result = new ErrorResult();
                    result.setErrorCode(ErrorCode.SYSTEM_ERROR);
                    result.setErrorMsg(e.getLocalizedMessage());
                    result.setErrorIndex(i+2);
                    errors.add(result);
                }
                if (errors != null && errors.size() > 0) {
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("isSuccess", false);
                    map.put("failNum", errors.size());
                    map.put("successNum", array.size() - errors.size());
                    map.put("errorMsgList", errors);
                    responseMsg.setData(map);
                    responseMsg.setMsg("SUCCESS");
                    return SUCCESS;
                }
            }
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        BatchCreateShipmentOrderCommand command = new BatchCreateShipmentOrderCommand();
        command.setShipmentVos(list);
        command.setImportExcelName(importExcelName);
        ModifyResponse response = lmaShipmentOrderService.batchCreateShipmentOrder(command);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("isSuccess", response.getIsSuccess());
        map.put("failNum", response.getFailNum());
        map.put("successNum", response.getSuccessNum());
        map.put("errorMsgList", response.getErrorResultList());
        map.put("importExcelName", importExcelName);
        responseMsg.setData(map);
        responseMsg.setMsg("SUCCESS");
        return SUCCESS;
    }

    /**
     * 获取订单列表信息
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/getOrderList")
    public String getOrderList(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        QueryShipmentOrderBySellerCommand command = null;
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            command = LMAShipmentOrderBuilder.buildQueryShipmentOrderBySeller(json);
            Searchable<?> searchable = buildSearchablePageParams(json);
            command.setSearchable(searchable);
        } else if (obj == null || StringUtils.isBlank(obj.toString())) {
            command = new QueryShipmentOrderBySellerCommand();
            Searchable<?> searchable = buildSearchablePageParams(null);
            command.setSearchable(searchable);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        Page<LMAShipmentOrderVo> pagelist = lmaShipmentOrderService.getOrderListBySeller(command);
        if (pagelist != null) {
            List<Map<String, Object>> datalist = LMAShipmentOrderBuilder.buildGetOrderListBySeller(pagelist.getContent());
            Map<String, Object> pageMap = getPageParams(pagelist);
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("list", datalist);
            data.put("pageParams", pageMap.get("pageParams"));
            responseMsg.setData(data);
        }
        return SUCCESS;
    }

    @RequestMapping(value = "/exportOrderList")
    public String exportOrderList(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        QueryShipmentOrderBySellerCommand command = null;
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            command = LMAShipmentOrderBuilder.buildQueryShipmentOrderBySeller(json);
            Searchable<?> searchable = buildSearchablePageParams(json);
            command.setSearchable(searchable);
        } else if (obj == null || StringUtils.isBlank(obj.toString())) {
            command = new QueryShipmentOrderBySellerCommand();
            Searchable<?> searchable = buildSearchablePageParams(null);
            command.setSearchable(searchable);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        Page<LMAShipmentOrderVo> pagelist = lmaShipmentOrderService.exportOrderListBySeller(command);
        if (pagelist != null) {
            List<Map<String, Object>> datalist = LMAShipmentOrderBuilder.buildExportOrderListBySeller(pagelist.getContent());
            Map<String, Object> pageMap = getPageParams(pagelist);
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("list", datalist);
            data.put("pageParams", pageMap.get("pageParams"));
            responseMsg.setData(data);
        }
        return SUCCESS;
    }

    /**
     * 批量打印面单
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/batchPrint")
    public String batchPrint(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        // 批量打印接口
        Object obj = requestMsg.getData();
        List<String> winitOrderNOs = new ArrayList<String>();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            if (!json.containsKey("winitOrderNOs")) {
                throw new ApiException(ErrorCode.ORDER_NOT_NULL, "data");
            }

            JSONArray arr = json.getJSONArray("winitOrderNOs");
            for (int i = 0; i < arr.size(); i++) {
                String j = arr.getString(i);
                winitOrderNOs.add(j);
            }
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        BatchPrintShipmentOrderCommand command = new BatchPrintShipmentOrderCommand();
        command.setCtx(CommandContext.getContext());
        command.setOrderNos(winitOrderNOs);

        List<LMAShipmentOrderLabelVo> labelVos = lmaShipmentOrderService.batchPrintShipmentOrderLabel(command);

        Map<String, Object> dataMap = new HashMap<String, Object>();

        if (labelVos == null || labelVos.size() == 0) {
            throw new ApiException(ErrorCode.LABEL_NOT_CREATE);
        } else {
            int successCount = 0;
            int failedCount = 0;
            List<String> labelPaths = new ArrayList<String>();
            List<String> winitOrderNos = new ArrayList<String>();
            for (LMAShipmentOrderLabelVo labelVo : labelVos) {
                if ("FD".equals(labelVo.getLabelStatus())) {
                    failedCount++;
                    continue;
                }
                successCount++;
                labelPaths.add(labelVo.getLabelUrl());
                winitOrderNos.add(labelVo.getOrderNo());
            }

            if (labelPaths.size() == 0) {
                throw new ApiException(ErrorCode.LABEL_NOT_CREATE);
            }

            dataMap.put("successCount", successCount);
            dataMap.put("failedCount", failedCount);
            if (labelPaths.size() == 1) {
                WinitLabelParser parser = new WinitLabelParser();
                List<String> files = new ArrayList<String>();
                files.add(parser.getWinitLabelByURL(labelPaths.get(0)));
                dataMap.put("files", files);
            } else {
                dataMap.put("files", mergeLabels(labelPaths));
            }
            dataMap.put("winitOrderNo", winitOrderNos);
            responseMsg.setData(dataMap);
        }

        return SUCCESS;

    }

    /**
     * 多面单合并成一个PDF
     * 
     * @param labelPaths 面单URL
     * @return
     */
    private List<String> mergeLabels(List<String> labelPaths) {
        List<String> files = null;
        if (null != labelPaths && labelPaths.size() > 0) {
            files = new ArrayList<String>();
            PDFBox4MergePDF tool = new PDFBox4MergePDF();
            String path = "";
            String os = System.getProperty("os.name");
            if (os.toLowerCase().startsWith("win")) {
                path = ApiConstant.PATH_MERGE_PDF_WIN + DateUtil.getCurrentDate() + ".pdf";
            } else {
                path = ApiConstant.PATH_MERGE_PDF_LINUX + DateUtil.getCurrentDate() + ".pdf";
            }
            try {
                tool.merge(labelPaths, path);
                files.add(tool.file2Base64(path));
            } catch (Exception e) {
                logger.error("WinitLableController->query :error:{}", e.getLocalizedMessage());
            } finally {
                // 删除临时文件
                tool.delFile(path);
            }
        }
        return files;
    }

    /**
     * 批量打印面单
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/getOrder")
    public String getOrder(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        // 获取订单信息
        Object obj = requestMsg.getData();
        String winitOrderNo = null;
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            winitOrderNo = json.getString("winitOrderNo");
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        QueryShipmentOrderCommand command = new QueryShipmentOrderCommand();
        command.setOrderNo(winitOrderNo);
        LMAShipmentOrderVo vo = lmaShipmentOrderService.getOrderDetailByOrderNo(command);
        if (vo != null) {
            Map<String, Object> data = LMAShipmentOrderBuilder.buildOrderDetailListByOrderNo(vo);
            responseMsg.setData(data);
        }
        return SUCCESS;
    }

    /**
     * 批量查询查询导入失败的订单信息
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/getImportOrderListLog")
    public String getImportOrderListLog(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        // 查询导入日志
        QueryShipmentOrderLogBySellerCommand command = null;
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            command = LMAShipmentOrderLogBuilder.buildQueryShipmentOrderLogBySeller(json);
            Searchable<?> searchable = buildSearchablePageParams(json);
            command.setSearchable(searchable);
        } else if (obj == null || StringUtils.isBlank(obj.toString())) {
            command = new QueryShipmentOrderLogBySellerCommand();
            Searchable<?> searchable = buildSearchablePageParams(null);
            command.setSearchable(searchable);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        Page<LMAShipmentOrderImportLogVo> pagelist = lmaShipmentOrderService.getOrderLogListBySeller(command);
        if (pagelist != null) {
            List<Map<String, Object>> datalist = LMAShipmentOrderLogBuilder.buildGetOrderLogListBySeller(pagelist.getContent());
            Map<String, Object> pageMap = getPageParams(pagelist);
            Map<String, Object> data = new HashMap<String, Object>();
            data.put("list", datalist);
            data.put("pageParams", pageMap.get("pageParams"));
            responseMsg.setData(data);
        }
        return SUCCESS;
    }
    
    /**
     * 根据挂号查询订单详情
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/queryOrderByTrackingNo")
    public String queryOrderByTrackingNo(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        String trackingNo = null;
        String orderNo = null;
        if (obj instanceof JSONObject) {
            JSONObject jsonObj = (JSONObject) obj;
            // build
            trackingNo = jsonObj.getString("trackingNo");
            orderNo = jsonObj.getString("orderNo");
            // validate
            if (StringUtils.isNotBlank(trackingNo)) {
                ValidateUtil.validMaxLength(jsonObj, "trackingNo", 40);
            } else if (StringUtils.isNotBlank(orderNo)) {
                ValidateUtil.validMaxLength(jsonObj, "orderNo", 128);
            }
            // 清除当前登录用户信息,给予可查看所有订单信息的权限
            CommandContext.getContext().set(ApiConstant.API_BPARTNER, null);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        QueryShipmentOrderByTrackingNoCommand command = new QueryShipmentOrderByTrackingNoCommand();
        command.setTrackingNo(trackingNo);
        command.setOrderNo(orderNo);
        command.setCtx(CommandContext.getContext());
        LMAShipmentOrderWorkOrderHelpVo helpVo = lmaShipmentOrderService.queryLmaOrderByTrackingNoOrORderNo(command);
        //组装页面返回参数
        responseMsg.setData(LMAShipmentOrderBuilder.buildReturnLmaOrderInfoByWorkOrder(helpVo));
        return SUCCESS;
    }
}
